<template>
  <a-modal
    :title="formTitle"
    :visible="open"
    @cancel="cancel"
    width="80%"
    centered
    footer= ""
  >
    <a-form-model
      ref="formRef"
      :model="form"
      :rules="formRules"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 16 }"
    >
      <a-form-model-item prop="type" :label="$t('all.autotext1573')">
        <div
          v-for="(item,id) in radioList"
          :key="id"
          v-bind:class="radioCssArray[id]"
          @click="radiobutton(id)"
        >
          <div class="radiotitle">
            <div class="radiotitlecontent">
              {{ item.title }}
            </div>
            <div class="radioicon" v-if="radioCssArray[id].isActive">
              <a-icon type="check" />
            </div>
          </div>
          <div class="radiomessage">
            {{ item.message }}
          </div>
        </div>
      </a-form-model-item>
      <a-form-model-item prop="title" :label="$t('all.autotext1574')">
        <a-input v-model="form.title" :placeholder="$t('all.autotext1575')"/>
      </a-form-model-item>
      <a-form-model-item prop="content" :label="$t('all.autotext1576')">
        <quill-editor  ref="myQuillEditor"
                       :content="form.content"
                       @editChange="quillChange($event)"
                       @updateLength="quillLength($event)"
        ></quill-editor>
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ offset: 4 }" style="margin-top:20px;">
        <a-button @click="cancel" style="margin-right: 10px">
          {{ $t('all.autotext1578') }}
        </a-button>
        <a-button type="primary" @click="handleSubmit">
          {{ $t('all.autotext1579') }}
        </a-button>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import quillEditor from '@/components/quillEditor/quill-editor'
import { savePc } from '@/api/feedback/feedback'

export default {
  components: { quillEditor },
  data () {
    return {
      formTitle: this.$t('all.autotext1587'),
      open: false,
      form: {
        id: undefined,
        type: 0,
        title: undefined,
        content: undefined,
        pictures: undefined
      },
      radioCssArray: [],
      radioList: [
        {
          id: 0,
          title: this.$t('all.autotext1588'),
          message: this.$t('all.autotext1589')
        },
        {
          id: 1,
          title: this.$t('all.autotext1590'),
          message: this.$t('all.autotext1591')
        },
        {
          id: 2,
          title: this.$t('all.autotext1592'),
          message: this.$t('all.autotext1593')
        }
      ],
      formRules: {
        type: [{ required: true, message: this.$t('all.autotext1594'), trigger: 'blur' }],
        title: [{ required: true, message: this.$t('all.autotext1575'), trigger: 'blur' }],
        content: [{ required: true, message: this.$t('all.autotext1577'), trigger: 'blur' }]
      }
    }
  },
  mounted () {
  },
  created () {
    this.reset()
  },
  watch: {
    data (val) {

    }
  },
  methods: {
    handleAdd () {
      this.open = true
      this.form.type = 0
    },
    handleUpdate (record) {
      this.open = true
      this.formTitle = this.$t('all.autotext1684')
      this.form = {
        id: record.id,
        type: record.type,
        title: record.title,
        content: record.content,
        pictures: record.pictures
      }
      this.radioCssArray = []
      /** 初始化数组数据 **/
      for (let i = 0; i < this.radioList.length; i++) {
        let radioItem = {}
        if (i === record.type) {
          radioItem = { radiocss: false, radiocssactive: true, isActive: true }
        } else {
          radioItem = { radiocss: true, radiocssactive: false, isActive: false }
        }
        this.radioCssArray.push(radioItem)
      }
    },
    reset () {
      /** 初始化数组数据 **/
      for (let i = 0; i < this.radioList.length; i++) {
        let radioItem = {}
        if (i === 0) {
          radioItem = { radiocss: false, radiocssactive: true, isActive: true }
        } else {
          radioItem = { radiocss: true, radiocssactive: false, isActive: false }
        }
        this.radioCssArray.push(radioItem)
      }
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    radiobutton (id) {
      let radioCssA = this.radioCssArray
      let radioNew = []
      for (let o in radioCssA) {
        // eslint-disable-next-line no-unused-vars
        let radioItem = radioCssA[o]
        let radioNewItem = { radiocss: true, radiocssactive: false, isActive: false }
        radioNew.push(radioNewItem)
      }
      radioNew[id] = { radiocss: false, radiocssactive: true, isActive: true }
      this.radioCssArray = radioNew
      /** 将选中的type给到保存参数 **/
      this.form.type = id
    },
    handleSubmit () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          let param = this.form
          savePc(param).then((res) => {
            this.$message.success(
              this.$t('all.autotext1595'))
            this.$emit('ok')
            this.cancel()
          })
            .catch(() => {})
        }
      })
    },
    quillChange (e) {
      this.form.content = e
    },
    quillLength (e) {
      this.contentLength = e
    },
    updateCover (val) {
      this.form.videoCover = val
    }
  }
}
</script>
<style lang="less" scoped>
  .radiocss {
    width: 200px;
    height: 124px;
    background: #FFFFFF;
    border-radius: 8px;
    border: 1px solid #DDE2ED;
    padding: 0;
    float: left;
    margin:0 0 10px 10px;
    cursor:pointer;
  }
  .radiocssactive {
    width: 200px;
    height: 124px;
    background: #FFFFFF;
    border-radius: 8px;
    border: 1px solid #26C4D9;
    padding: 0;
    float: left;
    color: #26C4D9;
    margin:0 0 10px 10px;
    cursor:pointer;
  }
  .radiotitle {
    height: 37px;
  }
  .radiomessage {
    font-size: 12px;
    padding-left: 10px;
    line-height: 24px;
  }
  .radiotitle .radiotitlecontent {
    float: left;
    font-size: 14px;
    font-family: PingFangSC-Medium, PingFang SC;
    line-height: 37px;
    padding-left: 10px ;
  }
  .radiotitle .radioicon {
    float: right;
    width: 24px;
    height: 24px;
    text-align: center;
    line-height: 24px;
    color: #ffffff;
    background: #26C4D9;
    border-radius: 0px 8px 0px 8px;
  }
  .line{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .radio-box{
    display: inline-block;
    position: relative;
    height: 25px;
    line-height: 25px;
    margin-right: 5px;
    flex-basis: 50%;
    display: flex;
    align-items: center;
  }
  .radio:before{
    content: '';
  }
  .radio {
    display: inline-block;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    border: 2px solid #ffffff;
    margin-right: 0.25rem;
  }
  .input-radio {
    display: inline-block;
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
    left: 0px;
    outline: none;
    -webkit-appearance: none;
  }
  .on::after {
    content: '';
    width: 11px;
    height: 11px;
    background: #f2b449;
    position: absolute;
    border-radius: 50%;
    top: 7px;
    left: 4px;
  }
  .btn {
    height: 2.2rem;
    width: 5.5rem;
    margin: 2rem auto;
    background-color: bisque;
    border-radius: 2rem;
    line-height: 2.2rem;
    text-align: center;
  }
  /deep/.my-quill-editor {
    margin-top: -10px;
  }
  /deep/.ql-editor {
    padding: 0 !important;
    .ql-container {
      padding: 6px;
    }
  }

</style>
